package chances.webui.taglibs;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.PageContext;

import chances.webui.annotation.Widget;

public class WebPageContext {
	public final static String NAME = WebPageContext.class.getSimpleName();
	private List<Object>  components = new ArrayList<Object>();

	public List<Object> getComponents() {
		return components;
	}
	
	public List<Widget> getWidgets(){
		List<Widget> result = new ArrayList<Widget>();
		this.components.forEach(s->{
			if(s.getClass().isAnnotationPresent(Widget.class)){
				Widget annotation = s.getClass().getAnnotation(Widget.class);
				result.add(annotation);
			}
		});
		return result;
	}

	public void addComponent(Object obj){
		this.components.add(obj);
	}
	
	
	public static WebPageContext getWebPageContext(PageContext pageContext ){
		WebPageContext webPageContext = (WebPageContext) pageContext.findAttribute(NAME);
		if(webPageContext==null){
			 webPageContext = new WebPageContext();
			 pageContext.setAttribute(NAME,webPageContext);
		}
		return webPageContext;
	}
	
}
